package com.hhf.rpa.ScreenRecorderTest;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import com.github.kwhat.jnativehook.mouse.NativeMouseEvent;
import com.github.kwhat.jnativehook.mouse.NativeMouseListener;
import lombok.extern.slf4j.Slf4j;


/**
 * 监控鼠标和键盘操作
 */
@Slf4j
public class JNativeHookExample implements NativeKeyListener, NativeMouseListener {

    private Long mouseClickCount = 0L;
    private Long keyClickCount = 0L;

    public static void main(String[] args) {
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(new JNativeHookExample());
        GlobalScreen.addNativeMouseListener(new JNativeHookExample());
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        keyClickCount ++;
        System.out.println(keyClickCount + ":Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode())+ ", code:"+e.getKeyCode());
    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {
        System.out.println(keyClickCount + ":Key Typed: " + e.getKeyText(e.getKeyCode()) + ", code:"+e.getKeyCode());
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println(keyClickCount + ":Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode())+ ", code:"+e.getKeyCode());
    }

    @Override
    public void nativeMousePressed(NativeMouseEvent e) {
        mouseClickCount ++;
        System.out.println(mouseClickCount + ":Mouse Pressed: " + e.getButton() + "，坐标："+e.getX()+", "+ e.getY());
    }

    @Override
    public void nativeMouseReleased(NativeMouseEvent e) {
        System.out.println(mouseClickCount + ":Mouse Released: " + e.getButton() + "，坐标："+e.getX()+", "+ e.getY());
    }

    @Override
    public void nativeMouseClicked(NativeMouseEvent e) {
        System.out.println(mouseClickCount + ":Mouse Clicked: " + e.getButton() + "，坐标："+e.getX()+", "+ e.getY());
    }

}
